package cn.liberals.order.service.impl;

import cn.liberals.order.domain.OrderPetAcquisition;
import cn.liberals.order.dto.OrderAcQuisition;
import cn.liberals.order.mapper.OrderPetAcquisitionMapper;
import cn.liberals.order.service.IOrderPetAcquisitionService;
import cn.liberals.basic.service.impl.BaseServiceImpl;
import cn.liberals.pet.domain.Pet;
import cn.liberals.pet.domain.SearchMasterMsg;
import cn.liberals.pet.mapper.PetMapper;
import cn.liberals.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liberals
 * @since 2022-07-19
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public void confirm(OrderAcQuisition orderAcQuisition) {
        // 1.修改订单状态、价格
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(orderAcQuisition.getId());
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(orderAcQuisition.getMoney());
        orderPetAcquisition.setPaytype(orderAcQuisition.getPayType());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 2.如果要在此处让用户填写宠物信息，那么就需要修改宠物基本信息，我们此处就只修改宠物成本价
        Pet pet = petMapper.loadById(orderPetAcquisition.getPetId());
        pet.setCostprice(orderAcQuisition.getMoney());
        petMapper.update(pet);
        // 3.修改寻主消息状态为已完成
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setState(2);
        searchMasterMsgMapper.update(searchMasterMsg);
    }

    /*
    * @Title: cancelOrder
    * @Description: 编写取消订单接口
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/24 0:07
    * @Parameters:
    * @Return
    */
    @Override
    public void cancelOrder(Long id) {
        // 1.根据订单Id查询订单，修改订单状态 -1
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 2.查询该订单对应的寻主消息，把shopId置空，放回寻主池
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
        // 3.删除宠物
        petMapper.remove(orderPetAcquisition.getPetId());
    }
}
